package cc.eddic.practice.concurrency;

import cc.eddic.practice.concurrency.impl.*;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class ConcurrencyLxApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(ConcurrencyLxApplication.class,args);
    }

    public void run(String... args) throws Exception{
//        GzFileUtilImpl gzFileUtil=new GzFileUtilImpl();
//
//        gzFileUtil.readUtil("D:\\索尼培训\\2023-04");
//        度压缩文件
        LogGZParseImpl logGZParse=new LogGZParseImpl();
        logGZParse.parse("D:\\索尼培训\\output");

//        CompletableFuture异步
//        LogParser logParser=new LogParseCompletableFutureImpl();
//        logParser.parse("D:\\\\索尼培训\\\\上机考试\\\\nginx-logs");
//        有返回值Future的多线程
//        LogParser logParser=new LogParseFutureImpl();
//        logParser.parse("D:\\\\索尼培训\\\\上机考试\\\\nginx-logs");
//        elapse("executor",this::executor);
        /*long start=System.currentTimeMillis();
        log.debug("start");
        LogParser logParser=new LogParseExecutorLxImpl();
        logParser.parse("D:\\索尼培训\\上机考试\\nginx-logs");
        log.debug("elapsed {} ms",System.currentTimeMillis()-start);*/

        //        打印日志
//        if(log.isInfoEnabled()) log.info("logMap is {}",newMap.size());
//        int count=0;int validNum=0;
//        for(String s:newMap.keySet()){
//            count+=newMap.get(s).size();
//            validNum+=newMap.get(s).stream().filter(LogItem::isValidRequest).count();
//        }            if(log.isInfoEnabled()) log.info("logMapCOunt is {},shai is {}",count,validNum);
//        loop();
//        executor();
//        log.debug("elapsed {} ms",System.currentTimeMillis()-start);

    }
    private void  elapse(String taskName,Runnable runnable){
        long start = System.currentTimeMillis();
        log.debug("start",taskName);
        try{
            runnable.run();
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        log.debug("[} elapsed {} ms",taskName,System.currentTimeMillis()-start);
    }
    private void executor(){
//        分配线程池
        ExecutorService executorService= Executors.newFixedThreadPool(8);
        CountDownLatch latch=new CountDownLatch(LOG_FILE_COUNT);
        for(int i=0;i<LOG_FILE_COUNT;i++){
            int finalI=i;
            if (log.isDebugEnabled()) log.debug("submit {}", finalI);
            executorService.submit(()->dummyParse(finalI,latch));
        }
        try{
            latch.await();
        }catch (InterruptedException e){
            log.error(e.getMessage());
        }
        ExecutorServiceUtil.shutdown(executorService);
    }
    private void loop(){
        for(int i=0;i<LOG_FILE_COUNT;i++){
            dummyParse(i);
        }
    }
    private void dummyParse(int i,CountDownLatch latch){
        dummyParse(i);
        latch.countDown();

    }
    private void dummyParse(int i){
        try{
            val r=rnd(10,50);
            if(log.isInfoEnabled()) log.debug("{}=>sleep {} ms",i,r);
            Thread.sleep(r);
        }catch (InterruptedException e){
            log.error(e.getMessage());
        }
    }
    private int rnd(int min,int max){
        return random.nextInt(max-min+1)+min;
    }
    private final int LOG_FILE_COUNT=256;
    private final Random random=new Random(System.currentTimeMillis());
    public void parse(String... args) throws Exception{
        long start=System.currentTimeMillis();
//        if(log.isInfoEnabled()) log.info("HEllo world");
//        LogItemParser logItemParser=new LogItemParseLxImpl();
//        LogItem logItem=logItemParser.parse("54.146.144.38 - - [04/Jul/2023:00:57:52 +0800] \"GET / HTTP/1.1\" 444 0 \"-\" \"Mozilla/5.0 (Windows NT 6.2;en-US) AppleWebKit/537.32.36 (KHTML, live Gecko) Chrome/55.0.3036.80 Safari/537.32\"").get();

//        if(log.isInfoEnabled()) log.info("HEllo world,{}",logItem.getTime());
        LogParser logParser=new LogParserLxImpl();

        Map<String, List<LogItem>> listMap=logParser.parse("D:\\索尼培训\\上机考试\\nginx-logs");
//        log.debug("aaaaaaaaaa{}",listMap.get("D:\\索尼培训\\上机考试\\access.log").size());
//        if(log.isDebugEnabled()){
//            for(LogItem l:listMap.get("D:\\索尼培训\\上机考试\\access.log")){
//                log.debug("{}",l);
//            }
//        }
    }
}
